package com.zhongfl.crm.service.impl.boss;

import com.github.pagehelper.PageHelper;
import com.zhongfl.crm.enums.boss.OrderAttachmentType;
import com.zhongfl.crm.lixiao.mapper.OrderAttachmentMapper;
import com.zhongfl.crm.model.boss.OrderAttachmentQuery;
import com.zhongfl.crm.service.boss.AttachmentService;
import com.zhongfl.crm.service.boss.OrderAttachmentService;
import com.zhongfl.crm.model.boss.OrderAttachment;
import org.springframework.stereotype.Service;
import tech.heptagram.channel.api.common.AssertUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wang.yuchuan
 * @date 2018/5/4
 */
@Service
public class OrderAttachmentServiceImpl implements OrderAttachmentService {

    @Resource
    private OrderAttachmentMapper attachmentMapper;

    @Resource
    private AttachmentService attachmentService;

    @Override
    public List<OrderAttachment> findByOrderNo(String orderNo) {
        return null;
    }

    @Override
    public List<OrderAttachment> findByFilePath(String filePath) {
        return null;
    }

    @Override
    public void deleteByFilePath(String filePath, String deleteBy) {
        AssertUtils.hasText(filePath, "要删除的filePath不能为空");
        AssertUtils.hasText(deleteBy, "删除人不能为空");

        attachmentMapper.deleteByFilePath(filePath, deleteBy);
    }

    @Override
    public void save(OrderAttachment orderAttachment) {
        attachmentMapper.insertSelective(orderAttachment);
    }

    @Override
    public List<OrderAttachment> findByQuery(OrderAttachmentQuery queryInfo) {
        PageHelper.startPage(queryInfo.getPage(), queryInfo.getPageSize());
        List<OrderAttachment> attachments = attachmentMapper.selectByQuery(queryInfo);
        for (OrderAttachment attachment : attachments) {
            attachmentService.getUrl(attachment);
        }
        return attachments;
    }

    @Override
    public List<OrderAttachment> findByOrderNoAndKey(String orderNo, OrderAttachmentType key) {
        return attachmentMapper.selectByOrderNoAndKey(orderNo, key);
    }

    /**
     * 通过workOrderNo和key查询
     *
     * @param workOrderNo workOrderNo
     * @param key         key
     * @return list
     */
    @Override
    public List<OrderAttachment> findByWorkOrderNoAndKey(String workOrderNo, OrderAttachmentType key) {
        return attachmentMapper.selectByWorkOrderNoAndKey(workOrderNo, key);
    }

    /**
     * 根据workOrderNo和跟进记录类型删除
     *
     * @param workOrderNo         workOrderNo
     * @param orderAttachmentType 跟进记录类型
     * @param staffName           当前登录人姓名（操作人）
     */
    @Override
    public void deleteByWorkOrderNoAndKey(String workOrderNo, OrderAttachmentType orderAttachmentType, String staffName) {
        attachmentMapper.deleteByWorkOrderNoAndKey(workOrderNo, orderAttachmentType, staffName);
    }
}
